package org.qy.star_server.data.manager;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.qy.star_server.core.*;
import org.qy.star_server.data.mapper.MetaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author yinger
 * @description TODO
 * @date 2024/3/26 09:59
 **/
@Slf4j
public class DBManager {
    public MetaMapper metaMapper;
    public DBManager(MetaMapper metaMapper) {
        this.metaMapper = metaMapper;
    }

    public boolean createUserTable() {
        try {
            metaMapper.createUserTable();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean insertUser(User user) {
        try {
            metaMapper.insertUser(user);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean deleteUser(String username) {
        try {
            metaMapper.deleteUser(username);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean updateUser(User user) {
        try {
            metaMapper.updateUser(user);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public User selectUser(String username) {
        try {
            User user = metaMapper.selectUser(username);
            if (user != null) {
                return user;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    public List<User> selectAllUser() {
        try {
            List<User> users = metaMapper.selectAllUser();
            if (users != null) {
                return users;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }


    // 虚拟机op
    public boolean createVirtualHostTable() {
        try {
            metaMapper.createVirtualHostTable();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean insertVirtualHost(VirtualHost virtualHost) {
        try {
            metaMapper.insertVirtualHost(virtualHost);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean deleteVirtualHost(VirtualHost virtualHost) {
        try {
            metaMapper.deleteVirtualHost(virtualHost);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public VirtualHost selectVirtualHost(VirtualHost virtualHost) {
        try {
            VirtualHost v = metaMapper.selectVirtualHost(virtualHost);
            if (v != null) {
                return v;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    public List<VirtualHost> selectAllVirtualHost() {
        try {
            List<VirtualHost> virtualHosts = metaMapper.selectAllVirtualHost();
            if (virtualHosts != null) {
                return virtualHosts;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }


    // 交换机op
    public boolean createExchangeTable() {
        try {
            metaMapper.createExchangeTable();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean insertExchange(Exchange exchange) {
        try {
            metaMapper.insertExchange(exchange);
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    public boolean deleteExchange(Exchange exchange) {
        try {
            metaMapper.deleteExchange(exchange);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public Exchange selectExchange(Exchange exchange) {
        try {
            Exchange e = metaMapper.selectExchange(exchange);
            if (e != null) {
                return e;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    public List<Exchange> selectAllExchange() {
        try {
            List<Exchange> exchanges = metaMapper.selectAllExchange();
            if (exchanges != null) {
                return exchanges;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    // 队列op
    public boolean createQueueTable() {
        try {
            metaMapper.createQueueTable();
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    /**
     * 插入队列时将队列的目录结构创建出来
     *
     * @param queue 队列
     * @return 是否成功
     */
    public boolean insertQueue(Queue queue) {
        try {
            metaMapper.insertQueue(queue);
            // 创建队列目录结构
            new FileManager().createQueueFiles(queue);
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    public Queue selectQueue(Queue queue) {
        try {
            Queue q = metaMapper.selectQueue(queue);
            if (q != null) {
                return q;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    public boolean deleteQueue(Queue queue) {
        try {
            metaMapper.deleteQueue(queue);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public List<Queue> selectAllQueue() {
        try {
            List<Queue> queues = metaMapper.selectAllQueue();
            if (queues != null) {
                return queues;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    // 绑定op
    public boolean createBindingTable() {
        try {
            metaMapper.createBindingTable();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean insertBinding(Binding binding) {
        try {
            metaMapper.insertBinding(binding);
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    public boolean deleteBinding(Binding binding) {
        try {
            metaMapper.deleteBinding(binding);
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    public Binding selectBinding(Binding binding) {
        try {
            Binding b = metaMapper.selectBinding(binding);
            if (b != null) {
                return b;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    public List<Binding> selectAllBinding() {
        try {
            List<Binding> bindings = metaMapper.selectAllBinding();
            if (bindings != null) {
                return bindings;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }
}
